package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.handler.exception.CustomException;
import com.hhxy.model.dto.AddTagDto;
import com.hhxy.model.dto.TagDto;
import com.hhxy.model.entity.Tag;
import com.hhxy.model.vo.TagVo;
import com.hhxy.service.TagService;
import com.hhxy.utils.BeanCopyUtils;
import com.hhxy.utils.response.AppHttpCodeEnum;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author ghp
 * @date 2023/4/29
 * @title
 * @description
 */
@Api(tags = {"后端接口", "标签管理", "内容管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/content/tag")
public class BeTagController {

    @Resource
    private TagService tagService;

    @GetMapping("/list")
    @ApiOperation(value = "分页条件查询标签")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "name", value = "标签名")
    })
    public ResponseResult list(Integer pageNum, Integer pageSize, String name) {
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return tagService.list(pageNum, pageSize, name);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "根据id删除标签", notes = "可以单个删除，可以批量删除（批量删除格式：1,2,3,4...）")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "ids", value = "标签id", dataType = "Long[]")
    public ResponseResult deleteByIds(@PathVariable Long[] ids) {
        return tagService.deleteByIds(ids);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询标签")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "标签id", dataTypeClass = Long.class)
    public ResponseResult getTagById(@PathVariable Long id) {
        if (Objects.isNull(id) || id <= 0) {
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return tagService.getTagById(id);
    }

    @PutMapping
    @ApiOperation(value = "根据id修改标签信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult updateTagById(@RequestBody @Validated TagDto tagDto) {
        return tagService.updateTagById(tagDto);
    }

    @PostMapping
    @ApiOperation(value = "新增标签")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult add(@RequestBody @Validated AddTagDto addTagDto) {
        return tagService.add(addTagDto);
    }

    @GetMapping("/listAllTag")
    @ApiOperation(value = "查询所有标签", tags = "文章管理")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult getAllTag(){
        List<Tag> tagList = tagService.list();
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tagList, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }


}
